package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.dto.BusinessPoolDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.service.BusinessService;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private ClueService clueService;
    /**
     * 新增商机
     */
    @PostMapping
    public Result add(@RequestBody Business business) {
        log.info("新增商机: {}" , business);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(1); // 待分配
        businessService.save(business);
        return Result.success();
    }

    /**
     *  查询商机
     */
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("查询参数: {}" , businessDto);
        PageResult<Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);
    }

    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机: {}, {}", businessId, userId);
        businessService.assignBusiness(businessId, userId);
        return Result.success();
    }

    /**
     * 根据id查询商机
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("查询商机: {}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    /**
     * 跟进商机
     */
    @PutMapping
    public Result trackBuisness(@RequestBody Business business) {
        log.info("跟进商机: {}", business);
        businessService.trackBuisness(business);
        return Result.success();
    }


    // 踢回公海（将商机状态修改为4）
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("将线索标记为伪线索, id: {}", id);
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }


    // 转客户
    /**转客户功能，类似于线索管理中的转商机，具体逻辑如下：
     - 根据商机ID查询上信息。
     - 更新商机状态。
     - 根据查询到的商机信息，组装客户信息，保存客户信息。
     */
    @PutMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("将商机转成客户, id: {}", id);
        Business business = businessService.getBusinessById(id);
        business.setStatus(5);
        businessService.updateById(business);
        return Result.success();
    }

    // 线索池列表查询
    @GetMapping("/pool")
    public Result poolList(BusinessPoolDto businessPoolDto) {
        log.info("查询线索池列表: {}", businessPoolDto);
        PageResult<Business> pageResult = businessService.getBusinessesPoolByPage(businessPoolDto);
        return Result.success(pageResult);
    }
}
